package com.allianz.webapps.controllers;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.allianz.webapps.https.interfaces.appGetCoverBenefits.AppGetCoverBenefits;
import com.allianz.webapps.https.interfaces.appGetCoverBenefits.pojo.AppGetCoverBenefitsResponse;
import com.allianz.webapps.https.interfaces.util.HttpsUtil;
import com.allianz.webapps.orderspojo.Cover;
import com.fasterxml.jackson.databind.ObjectMapper;

@Controller
@RequestMapping("main")
public class AppGetCoverBenefitsController {
	protected final Log log = LogFactory.getLog(getClass());
	@Autowired
	private ObjectMapper objectMapper;
	@Autowired
	private AppGetCoverBenefits appGetCoverBenefits;
	/**
	 * 利益演示
	 * @param jsonParam
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("appgetcoverbenefits")
	@ResponseBody
	public List<AppGetCoverBenefitsResponse> appGetCoverBenefits(@RequestParam(value="jsonParam") String jsonParam,HttpServletRequest request,HttpServletResponse response) throws Exception{
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		Date date = new Date();
		Long start = date.getTime();
		
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "No-cache");
		response.setHeader("Cache-Control", "no-store");
		response.setDateHeader("Expires", 0);
		Cover cover=objectMapper.readValue(jsonParam,Cover.class);
		String body1=HttpsUtil.httpsPost(appGetCoverBenefits.requestAppGetCoverBenefitsParam(cover));
		JSONObject json=new JSONObject(body1);
		String body="";
		if (json.get("Body").equals(null)){
			if(!"S".equals(json.getString("Status")))
				throw new Exception(json.getString("ErrMsg"));
			else
				return null;	
		}else {
			body=json.getString("Body");
			List<AppGetCoverBenefitsResponse> appGetCoverBenefitsResponses=objectMapper.readValue(body, List.class);
			Long end = new Date().getTime();
			log.debug(df.format(date)+"========appgetcoverbenefits.action================time:"+(end-start)+"毫秒");
			return appGetCoverBenefitsResponses;
		}
		
		
	}
}
